﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Shsc.Net.FrameWork.Common.Util;

namespace Shsc.Net.FrameWork.Common.Validator
{
    /// <summary>
    /// 直接长度Check
    /// </summary>
    public class ByteLengthAttribute : BaseCheckAttribute
    {
        private int _length;

        /// <summary>
        /// 长度Check
        /// </summary>
        public int Length
        {
            get { return _length; }
        }

        public ByteLengthAttribute(int length)
        {
            this._messageId = "ERR_90002";
            _length = length;
        }

        /// <summary>
        /// 必須Check方法
        /// </summary>
        /// <param name="msgId"></param>
        /// <param name="msgParams"></param>
        public ByteLengthAttribute(int length, string msgId, params string[] msgParams)
        {
            this._messageId = msgId;
            this._msgParams = msgParams;
            _length = length;
        }

        public override string DoCheck(object obj, string fieldName)
        {
            if (obj == null)
            {
                return string.Empty;
            }
            int length = Encoding.UTF8.GetBytes(obj as string).Length;
            if (length > _length)
            {
                IList<string> lisParam = new List<string>();
                lisParam.Add(fieldName);
                lisParam.Add(_length.ToString());
                if (_msgParams != null)
                {
                    lisParam.Concat(this._msgParams);
                }
                return MessageUtil.GetSingleMessage(this._messageId, lisParam.ToArray());
            }
            return string.Empty;
        }
    }
}
